﻿using DefPack;
using DefPack.Common;
using SaveSation.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SaveSation
{
    public partial class ProcessForm : Form
    {
        int m_Tick = 0;
        public ProcessForm(DefPackArchive zipStream)
        {
            InitializeComponent();
            DefPackArchive m_ZipStream = zipStream;
            m_Tick = Environment.TickCount;
            m_ZipStream.OnProgressChanged += new ProgressChangedEventHandler(progressChanged);
            m_ZipStream.OnProgressException += new ProgressExceptionEventHandler(progressException);
            m_ZipStream.OnProgressCompleted += new ProgressCompletedEventHandler(progressCompleted);
            this.Show();
        }
        public ProcessForm(out ProgressChangedEventHandler eProgressChanged, out ProgressCompletedEventHandler eProgressCompleted, out ProgressExceptionEventHandler eProgressException)
        {
            InitializeComponent();
            m_Tick = Environment.TickCount;
            //eProgressChanged = new ProgressChangedEventHandler(progressChanged);
            eProgressChanged = new ProgressChangedEventHandler(progressChanged);
            eProgressException = new ProgressExceptionEventHandler(progressException);
            eProgressCompleted = new ProgressCompletedEventHandler(progressCompleted);
            this.Show();
        }
        private void progressChanged(object? sender, ProgressChangedEventArgs e)
        {
            int tick = (Environment.TickCount - m_Tick) / 1000;
            if (e.UserState != null)
                label4.Text = e.UserState.ToString();
            label3.Text = e.ProgressPercentage + " %";
            label5.Text = e.OverallProgressPercentage + " %";
            progressBar1.Value = e.ProgressPercentage < 0 ? 0 : e.ProgressPercentage > 100 ? 100 : e.ProgressPercentage;
            progressBar2.Value = e.OverallProgressPercentage < 0 ? 0 : e.OverallProgressPercentage > 100 ? 100 : e.OverallProgressPercentage;
            label6.Text = formTime(tick);
            Application.DoEvents();
        }
        private void progressException(object? sender, ProgressExceptionEventArgs e)
        {
            Debug.WriteLine(e.Exception.Message);
            e.Exception.Error();
            this.Close();
        }
        private void progressCompleted(object? sender, ProgressCompletedEventArgs e)
        {
            label3.Text = "100 %";
            label5.Text = "100 %";
            progressBar1.Value = 100;
            progressBar2.Value = 100;
            Application.DoEvents();
            this.Close();
        }
        public static string formTime(int tickTime)
        {
            int houer = 0, minute = 0, second = 0;
            if (tickTime < 0)
                tickTime = 0;
            houer = tickTime / 3600;
            minute = (tickTime - houer * 3600) / 60;
            second = tickTime - houer * 3600 - minute * 60;
            string tmp;
            if (houer < 10)
                tmp = "0" + houer;
            else
                tmp = houer.ToString();
            if (minute < 10)
                tmp = tmp + ":0" + minute;
            else
                tmp = tmp + ":" + minute;
            if (second < 10)
                tmp = tmp + ":0" + second;
            else
                tmp = tmp + ":" + second;
            return tmp;
        }
        private static string formTime(string tickTime)
        {
            return formTime(int.Parse(tickTime));
        }

    }
}
